import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/auth.dart';
import 'package:kxy_flutter_app/helper/http.dart';
import 'package:kxy_flutter_app/r.dart';

class NavigationModel with ChangeNotifier {
  int _index = 0;
  int get index => _index;
  int noticeCount = 0;
  int certCount = 0;
  final PageController pageController = new PageController(initialPage: 0);

  List<NavigationItem> _items = new List<NavigationItem>();
  List<NavigationItem> get items => _items;

  NavigationModel() {
    rebuildNavItems();
  }

  void rebuildNavItems() {
    _items.clear();
    _items.add(NavigationItem(
      title: '首页',
      icon: R.assetsMenuHome,
      activeIcon: R.assetsMenuHomeCheck,
    ));
    _items.add(NavigationItem(
      title: '新闻',
      icon: R.assetsMenuNews,
      activeIcon: R.assetsMenuNewsCheck,
    ));
    _items.add(NavigationItem(
      title: '考试',
      icon: R.assetsMenuExam,
      activeIcon: R.assetsMenuExamCheck,
    ));
    _items.add(NavigationItem(
      title: '课程',
      icon: R.assetsMenuCourse,
      activeIcon: R.assetsMenuCourseCheck,
    ));
    _items.add(NavigationItem(
      title: '我的',
      icon: R.assetsMenuMy,
      activeIcon: R.assetsMenuMyCheck,
    ));
  }

  Future changeSelectIndex(int index) async {
    this._index = index;
    pageController.jumpToPage(_index);
    await refreshNoticeCount();
  }

  Future refreshNoticeCount({withNotify = true}) async {
    var res = await Http.instance.post('/news-app/notice/noreadcount', params: {
      'userId': Auth.instance.userId,
    });
    if (res.code == 200) {
      noticeCount = res.data['noticeNoReadCount'];
      certCount = res.data['certNoReadCount'];
    }

    if (withNotify) {
      notifyListeners();
    }
  }
}

class NavigationItem {
  final String title;
  final String icon;
  final String activeIcon;

  NavigationItem({
    this.title,
    this.icon,
    this.activeIcon,
  });
}
